﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using DAL;

namespace DACNPM.MyUserControl.UCHienThi
{
    public partial class UCSuaNV : UserControl
    {
        NhanVienBUO nvBUO = new NhanVienBUO();
        String idNV;
        int id;

        public UCSuaNV(String idNV)
        {
            InitializeComponent();
            dataGridView1.DataSource = nvBUO.GetAll();
            String a = idNV;
            lblIDNV.Text = idNV;
            tbl_NhanVien nv = nvBUO.GetSingle(a);
            txtTen.Text = nv.TenNhanVien;
            txtDiaChi.Text = nv.DiaChi;
            txtSDT.Text = nv.SoDienThoai;
            dateTimePicker1.Text = nv.NgaySinh;
        }

        public void binding()
        {
            lblIDNV.DataBindings.Clear();
            lblIDNV.DataBindings.Add("Text", dataGridView1.DataSource, "id_NhanVien");
            txtTen.DataBindings.Clear();
            txtTen.DataBindings.Add("Text", dataGridView1.DataSource, "TenNhanVien");
            dateTimePicker1.DataBindings.Clear();
            dateTimePicker1.DataBindings.Add("Text", dataGridView1.DataSource, "NgaySinh");
            txtSDT.DataBindings.Clear();
            txtSDT.DataBindings.Add("Text", dataGridView1.DataSource, "SoDienThoai");
            txtDiaChi.DataBindings.Clear();
            txtDiaChi.DataBindings.Add("Text", dataGridView1.DataSource, "DiaChi");
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            NumberBUO numBUO = new NumberBUO();
            if ((txtTen.Text.Trim() == "") || (txtDiaChi.Text.Trim() == "") || (txtSDT.Text.Trim() == ""))
            {
                MessageBox.Show("Chưa nhập đủ thông tin!");
            }
            else
            {
                if (numBUO.isNumberInput(txtSDT.Text) == false)
                {
                    MessageBox.Show("Trường số điện thoại phải nhập số!");
                }
                else
                {
                    tbl_NhanVien nv = new tbl_NhanVien();
                    nv.TenNhanVien = txtTen.Text;
                    nv.SoDienThoai = txtSDT.Text;
                    nv.DiaChi = txtDiaChi.Text;
                    nv.NgaySinh = dateTimePicker1.Text;
                    if (nvBUO.Update(lblIDNV.Text, nv) == true)
                    {
                        dataGridView1.DataSource = nvBUO.GetAll();
                        MessageBox.Show("Sửa nhân viên thành công");
                    }
                    else
                    {
                        MessageBox.Show("Có lỗi xảy ra khi sửa nhân viên");
                    }
                }
            }
            binding();
        }

        private void btnLamLai_Click(object sender, EventArgs e)
        {
            txtTen.Text = "";
            txtSDT.Text = "";
            txtDiaChi.Text = "";
        }

        private void radioTimTheoTen_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoTen.Enabled = true;
            txtTimTheoTen.Text = "";
            txtTimTheoMaNV.Enabled = false;
            txtTimTheoMaNV.Text = "";
            dataGridView1.Enabled = true;
            dataGridView1.DataSource = nvBUO.GetAll();
            binding();
        }

        private void radioTimTheoMaNV_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoTen.Enabled = false;
            txtTimTheoTen.Text = "";
            txtTimTheoMaNV.Enabled = true;
            txtTimTheoMaNV.Text = "";
            dataGridView1.Enabled = true;
            dataGridView1.DataSource = nvBUO.GetAll(); 
            binding();
        }

        private void txtTimTheoTen_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (radioTimTheoTen.Checked == true)
                {
                    if (txtTimTheoTen.Text.Trim().Equals(""))
                    {
                        MessageBox.Show("Chưa nhập tên nhân viên cần tìm");
                    }
                    else
                    {
                        List<tbl_NhanVien> list = nvBUO.searchByTenNV(txtTimTheoTen.Text.Trim());

                        if (list.Count > 0)
                        {
                            dataGridView1.AutoGenerateColumns = false;
                            dataGridView1.DataSource = list;
                            dataGridView1.Visible = true;
                        }
                        else
                        {
                            dataGridView1.Visible = true;
                            MessageBox.Show("Không có nhân viên cần tìm");
                        }
                    }
                }
            }
        }

        private void txtTimTheoMaNV_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (radioTimTheoMaNV.Checked == true)
                {
                    if (txtTimTheoMaNV.Text.Trim().Equals(""))
                    {
                        MessageBox.Show("Chưa nhập mã nhân viên cần tìm");
                    }
                    else
                    {
                        List<tbl_NhanVien> list = nvBUO.searchByMaNV(txtTimTheoMaNV.Text.Trim());

                        if (list.Count > 0)
                        {
                            dataGridView1.AutoGenerateColumns = false;
                            dataGridView1.DataSource = list;
                            dataGridView1.Visible = true;
                        }
                        else
                        {
                            dataGridView1.Visible = true;
                            MessageBox.Show("Không có nhân viên cần tìm");
                        }
                    }
                }
            }
        }

        private void txtTimTheoTen_TextChanged(object sender, EventArgs e)
        {
            List<tbl_NhanVien> list = nvBUO.searchByTenNV(txtTimTheoTen.Text.Trim());

            if (list.Count > 0)
            {
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = list;
                dataGridView1.Visible = true;
            }
        }

        private void txtTimTheoMaNV_TextChanged(object sender, EventArgs e)
        {
            List<tbl_NhanVien> list = nvBUO.searchByMaNV(radioTimTheoMaNV.Text.Trim());

            if (list.Count > 0)
            {
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = list;
                dataGridView1.Visible = true;
            }
        }

        private void radioXemTatCa_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoMaNV.Enabled = false;
            txtTimTheoMaNV.Text = "";
            txtTimTheoTen.Enabled = false;
            txtTimTheoTen.Text = "";
            dataGridView1.Enabled = true;
            dataGridView1.DataSource = nvBUO.GetAll();
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {

                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                    // Only RowHeader and Cells (which make up a row) should fire a menu
                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // Notice how I assign the menu to the dataGridView below...

                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = contextMenuStrip1;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));

                        id = dataGridView1.HitTest(e.X, e.Y).RowIndex;
                        idNV = dataGridView1.Rows[id].Cells[0].Value.ToString();


                    }
                    // and then I remove the menu here...
                    // This is because after we assign it to the dataGridView
                    // it allows you to right-click anywhere on the control to
                    // get a menu.  Try removing this line and expirement.
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                contextMenuStrip1.Visible = false;
            }
        }

        private void danhSáchNhânViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCDanhSachNV ucDSNV = new UCDanhSachNV();

            Form form = this.ParentForm;
            ControlAccess.ShowConTrol(form, ucDSNV, "grbHienThiChucNang");
        }

        private void thêmNhânViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCThemNV ucThemNV = new UCThemNV();

            Form form = this.ParentForm;
            ControlAccess.ShowConTrol(form, ucThemNV, "grbHienThiChucNang");
        }
    }
}
